package jdbmaplite.util;

import java.util.Arrays;
import java.util.Comparator;

public class StringUtils {

	/**
	 * Is Any Empty
	 * 
	 * @param strings
	 *            CharSequence
	 * @return boolean
	 */
	public static boolean isAnyEmpty(CharSequence... strings) {
		if (strings == null || strings.length <= 0) {
			return true;
		}
		for (CharSequence str : strings) {
			if (str == null || str.length() <= 0) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Format
	 * 
	 * @param str
	 *            String
	 * @param prefix
	 *            String
	 * @param argNames
	 *            String[]
	 * @param argValues
	 *            Object[]
	 * @return String
	 */
	public static String format(String str, String prefix, String[] argNames,
			Object[] argValues) {
		if (str == null) {
			return null;
		}
		int argsSize = 0;
		if (prefix == null || argNames == null || argValues == null
				|| str.length() <= 0 || prefix.length() <= 0
				|| (argsSize = argNames.length) <= 0
				|| argsSize != argValues.length) {
			return str;
		}
		// sort by arg name's length
		Object[][] indexArray = new Object[argsSize][2];
		for (int i = 0; i < argsSize; i++) {
			indexArray[i] = new Object[] { argNames[i], i };
		}
		Arrays.sort(indexArray, new Comparator<Object[]>() {
			public int compare(Object[] arg0, Object[] arg1) {
				return Integer.valueOf(((String) arg0[0]).length()).compareTo(
						Integer.valueOf(((String) arg1[0]).length()));
			}
		});
		// split to search list and replacement list
		String[] searchList = new String[argsSize];
		String[] replacementList = new String[argsSize];
		for (int i = argsSize - 1; i >= 0; i--) {
			// the search list order by it's string length desc
			int argIndex = (Integer) indexArray[i][1];
			String searchName = argNames[argIndex];
			if (searchName == null) {
				throw new IllegalArgumentException(
						"arg names can't contain the null value");
			}
			searchList[i] = prefix.concat(searchName);
			replacementList[i] = String.valueOf(argValues[argIndex]);
		}
		return org.apache.commons.lang.StringUtils.replaceEach(str, searchList,
				replacementList);
	}

	/**
	 * Format Each
	 * 
	 * @param str
	 *            String
	 * @param prefix
	 *            String
	 * @param searchList
	 *            String[]
	 * @param replacementList
	 *            Object[]
	 * @return String
	 */
	public static String formatEach(String str, String prefix,
			String[] searchList, Object[] replacementList) {
		if (str == null) {
			return null;
		}
		int searchListSize = 0;
		int replacementListSize = 0;
		if (prefix == null || searchList == null || replacementList == null
				|| str.length() <= 0 || prefix.length() <= 0
				|| (searchListSize = searchList.length) <= 0
				|| (replacementListSize = replacementList.length) <= 0) {
			return str;
		}
		if (searchListSize != replacementListSize) {
			throw new IllegalArgumentException(
					"searchList and replacementList's length are different.");
		}
		// merge it into an combined array
		String[][] combinedArray = new String[searchListSize][2];
		for (int i = 0; i < searchListSize; i++) {
			if (searchList[i] == null || replacementList[i] == null) {
				throw new IllegalArgumentException(
						"searchList or replacementList contains null value.");
			}
			combinedArray[i] = new String[] { searchList[i],
					String.valueOf(replacementList[i]) };
		}
		// the search list order by it's string length desc
		Arrays.sort(combinedArray, new Comparator<String[]>() {
			public int compare(String[] o1, String[] o2) {
				return compareStringLength(o1[0], o2[0], true, true);
			}
		});
		// split to the new list
		String[] newSearchList = new String[searchListSize];
		String[] newReplacementList = new String[searchListSize];
		for (int i = 0; i < searchListSize; i++) {
			String searchStr = combinedArray[i][0];
			if (!searchStr.startsWith(prefix)) {
				searchStr = prefix.concat(searchStr);
			}
			newSearchList[i] = searchStr;
			newReplacementList[i] = combinedArray[i][1];
		}
		return org.apache.commons.lang.StringUtils.replaceEach(str,
				newSearchList, newReplacementList);
	}

	/**
	 * Compare String Length
	 * 
	 * @param str1
	 *            String
	 * @param str2
	 *            String
	 * @param nullsAreHigh
	 *            boolean
	 * @param isOrderByDesc
	 *            boolean
	 * @return int
	 */
	public static int compareStringLength(String str1, String str2,
			boolean nullsAreHigh, boolean isOrderByDesc) {
		if (str1 == null && str2 == null) {
			return 0;
		} else if (str1 != null && str2 != null) {
			int str1Length = str1.length();
			int str2Length = str2.length();
			if (str1Length == str2Length) {
				return 0;
			} else if (str1Length < str2Length) {
				return (isOrderByDesc ? 1 : -1);
			}
			return (isOrderByDesc ? -1 : 1);
		} else if (str1 == null) {
			return (nullsAreHigh ? 1 : -1);
		}
		return (nullsAreHigh ? -1 : 1);
	}

}